package com.xl.service.impl;

import com.xl.property.CoreConstants;
import com.xl.service.UploadService;
import com.xl.utils.file.FileUtil;

import com.xl.utils.zip.ZipUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.StringReader;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UploadServiceImpl implements UploadService {

    @Override
    public void uploadFile(List<String> paths , HttpServletResponse response) {
        for (String path : paths) {
            if (!FileUtil.exist(path)) {
                throw new RuntimeException("文件不存在");
            }
        }

        //List<File> collect = paths.stream().map((str) -> {return new File(str) ;}).collect(Collectors.toList());
        //List<File> files = paths.stream().map((str) -> new File(str)).collect(Collectors.toList());

        //都存在开始压缩文件  把问价输出到 response里面
        ZipUtil.createZipToOutputStream(paths ,
                CoreConstants.fileDir+"/temp.zip" ,
                null ,
                response ,
                true);
    }

}
